﻿using System;
using System.Collections.Generic;
using System.Web.Http;
using eCommMongo.Core;
using eCommMongo.Core.Helper;
using eCommMongo.Service.Loggings;
using eCommMongo.Service.Products;
using yiCommerce.Core;
using yiCommerce.Service.Messages;

namespace eCommMongo.Web.Controllers.api
{
    public class ProductVariantAttributeSaveResult
    {
        public string Status { get; set; }
        public string Message { get; set; }
        public string Id { get; set; }
    }

    public class ProductInfoSaveResult
    {
        public string Status { get; set; }
        public string Message { get; set; }
        public string Id { get; set; }
    }

    //public class ProductInfoSaveRequest
    //{
    //    public int Id { get; set; }
    //    public string ProductName { get; set; }
    //    public string ShortDescription { get; set; }
    //    public string FullDescription { get; set; }

    //    public decimal Price { get; set; }
    //    public int StockQuantity { get; set; }

    //    public bool IsPublished { get; set; }

    //}

    public class ProductVariantInfoSaveResult
    {
        public string Status { get; set; }
        public string Message { get; set; }
        public int Id { get; set; }
    }

    public class ProductVariantInfoSaveRequest
    {
        public int Id{ get; set; }
        public int ProductId { get; set; }
        public string Name { get; set; }
        public string Sku{ get; set; }


        public decimal Price { get; set; }
        public int DisplayOrder { get; set; }
        public int StockQuantity { get; set; }

        public int PictureId { get; set;}
        public bool Published { get; set; }
        
    }


    public class ProductApiController : ApiController
    {

        private IProductService _productService;

        private IActivityService _activityService;

        private readonly IWorkContext _workContext;
        private IMessageService _messageService;


        public ProductApiController(IProductService productService, IActivityService activityService, IWorkContext workContext, IMessageService messageService)
        {
            _productService = productService;
            _activityService = activityService;
            _workContext = workContext;
            _messageService = messageService;
        }

        [HttpPost]
        public ProductDeleteResult DeleteProduct(ProductDeleteRequest request)
        {
            var result = new ProductDeleteResult();
            try
            {
                _productService.DeleteProduct(request.ProductId);


                result.Status = ConstInfo.ResultSuccessStatus;
                result.Message = _messageService.GetResourceValue(Messages.ProductDeleteSuccess, 0);

                //_activityService.InsertActivity("InsertProductVariantAttributeValue", JsonHelper.ToJson(request), _workContext.CurrentCustomer);
                return result;
            }
            catch (Exception e)
            {
                result.Status = ConstInfo.ResultFailedStatus;
                result.Message = e.Message;
                return result;
            }
        }


        [HttpPost]
        public ProductInfoSaveResult UpdateProductInfo(ProductInfoUpdateRequest request)
        {
            var result = new ProductInfoSaveResult();


            try
            {

                var updateProductInfoRequest = new ProductInfoUpdateRequest()
                {
                    Id = request.Id,
                    ProductName = request.ProductName,
                    FullDescription = request.FullDescription,
                    IsPublished = request.IsPublished,
                    ShortDescription = request.ShortDescription
                };
                var updateResult = _productService.UpdateProductInfo(updateProductInfoRequest);

                result.Status = ConstInfo.ResultSuccessStatus;
                result.Message = _messageService.GetResourceValue(Messages.ProductUpdateInfoSuccess, 0);
                result.Id = updateResult.Id;

                _activityService.InsertActivity("updateproductinfo", JsonHelper.ToJson(updateProductInfoRequest), _workContext.CurrentCustomer);
                //SuccessNotification(_localizationService.GetResource("Admin.Catalog.Products.Added"));

                return result;
            }
            catch (Exception e)
            {
                result.Status = ConstInfo.ResultFailedStatus;
                result.Message = e.ToString();
                return result;
            }

        }


        [HttpPost]
        public ProductInfoSaveResult InsertProductInfo(ProductInfoInsertApiRequest request)
        {
            var result = new ProductInfoSaveResult();


            try
            {

                var insertProductInfoRequest = new ProductInfoInsertRequest()
                    {
                        ProductName = request.ProductName,
                        FullDescription = request.FullDescription,
                        IsPublished = request.IsPublished,
                        ShopId = _workContext.CurrentCustomer.Shop.Id,
                        ShortDescription = request.ShortDescription,
                        Price = request.Price,
                        StockQuantity = request.StockQuantity,
                        PictureIds =new List<string>()
                            {
                              request.PictureId 
                            } 
                    };

                var insertResult = _productService.InsertProductInfo(insertProductInfoRequest);

               

                result.Status = "0";
                result.Id = insertResult.Id;
                result.Message = _messageService.GetResourceValue(Messages.ProductCreateInfoSuccess, 0);
                _activityService.InsertActivity("insertproductinfo", JsonHelper.ToJson(insertProductInfoRequest), _workContext.CurrentCustomer);
                //SuccessNotification(_localizationService.GetResource("Admin.Catalog.Products.Added"));
                return result;
            }
            catch (Exception e)
            {
                result.Status = ConstInfo.ResultFailedStatus;
                result.Message = e.ToString();
                return result;
            }

        }
        
    }
    public class ProductInfoInsertApiRequest
    {
        public string ShopId { get; set; }
        public string ProductName { get; set; }
        public string ShortDescription { get; set; }
        public string FullDescription { get; set; }

        public decimal Price { get; set; }
        public int StockQuantity { get; set; }

        public string PictureId { get; set; }
        public bool IsPublished { get; set; }

    }

    public class ProductDeleteRequest
    {
        public string ProductId { get; set; }
    }

    public class ProductDeleteResult
    {
        public string Status { get; set; }
        public string Message { get; set; }
    }
}
